package commRedisTag

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedis"
	"time"
)

type redisTagWriter struct {
}

func (e *redisTagWriter) writeTags(tagMap map[string]interface{}) error {
	if tagMap == nil || len(tagMap) == 0 {
		return errors.New("输入参数为空")
	}

	now := time.Now().UnixMilli()

	// 构造敏捷数据
	agileMap := make(map[string]interface{})
	for k := range tagMap {
		agileMap[k] = now
	}

	_, err := commRedis.Client.HMSet("fox-edge:tags:agile", agileMap).Result()
	if err != nil {
		return err
	}
	_, err = commRedis.Client.HMSet("fox-edge:tags:data", tagMap).Result()
	if err != nil {
		return err
	}
	_, err = commRedis.Client.Set("fox-edge:tags:sync", now, -1).Result()
	if err != nil {
		return err
	}

	return nil
}

func (e *redisTagWriter) deleteTags(entityTypes map[string]interface{}) error {
	if entityTypes == nil || len(entityTypes) == 0 {
		return errors.New("输入参数为空")
	}

	now := time.Now().UnixMilli()

	for k := range entityTypes {
		_, err := commRedis.Client.HDel("fox-edge:tags:agile", k).Result()
		if err != nil {
			return err
		}

		_, err = commRedis.Client.HDel("fox-edge:tags:data", k).Result()
		if err != nil {
			return err
		}

		_, err = commRedis.Client.Set("fox-edge:tags:sync", now, -1).Result()
		if err != nil {
			return err
		}
	}

	return nil
}
